package com.biomatch.controller;

import com.biomatch.domain.ContainerLock;
import com.biomatch.domain.Position;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.Pagination;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.PositionService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("lock.position")
@RequestMapping("/position")
@Api(value="位置管理")
public class PositionController {

    @Autowired
    private PositionService positionService;

    @Autowired
    private Internationalize internationalize;

    @RequestMapping(value="/get4Page", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取位置", response = UserInfo.class, httpMethod = "POST")
    public RestResult<Position> get4Page(@RequestBody Pagination pagination) {
        return RestResponse.success(positionService.get4Page(pagination));
    }

    @RequestMapping(value="/getAll", method = RequestMethod.POST)
    @ApiOperation(value = "获取全部位置", response = UserInfo.class, httpMethod = "POST")
    public RestResult<List<Position>> getAll() {
        return RestResponse.success(positionService.getAll());
    }

    @RequestMapping(value="/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "新增或更新位置", response = UserInfo.class, httpMethod = "POST")
    public RestResult<Position> saveOrUpdate(@RequestBody Position position) {
        Position result = positionService.saveOrUpdate(position);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("position.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value="/deletePosition", method = RequestMethod.POST)
    @ApiOperation(value = "删除位置", response = UserInfo.class, httpMethod = "POST")
    public RestResult<Position> delete(@RequestBody Position position) {
        Position result = positionService.delete(position);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("position.status.used"));
        } else {
            return RestResponse.success(result);
        }
    }
}